/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.iwords.teacher.web;

import com.iwords.ejb.LearningStatusEntityFacade;
import com.iwords.service.LearningStatusServiceLocal;
import com.iwords.service.object.LearningStatusEntity;
import com.iwords.service.object.TeacherEntity;
import com.iwords.service.object.UserEntity;
import com.iwords.service.persistence.UserPersistence;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author yunzi
 */
@WebServlet(name = "StudentServlet", urlPatterns = {"/student"})
public class StudentServlet extends BaseServlet<TeacherEntity> {
    
    @Override
    public void init(){
        
    }

    @EJB
    UserPersistence userFacade;
    
    /**如果command不能match到任何方法,默认显示的页面*/
    @Override
    protected void defaultAction(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //先查询当前老师的学生
        TeacherEntity teacher = (TeacherEntity) request.getSession(true).getAttribute("teacherInSession");
        System.out.println("StudentServlet.defaultAction() teacher.getTeachername() = "+teacher.getTeachername());
        List<UserEntity> students = userFacade.findByTeacherId(teacher.getId());
        System.out.println("StudentServlet.defaultAction() students = "+students);
        Map<Long, UserEntity> studentsMap = new HashMap<Long, UserEntity>();
        for(UserEntity student : students){
            studentsMap.put(student.getUserid(), student);
        }
        System.out.println("studentsMap = "+studentsMap);
        List<Object[]> statusList = learningStatusFacade.findLearningStatus(teacher.getId());
        for(Object[] status : statusList){
            System.out.println("status[0]="+status[0]+" status[0] instanceof Long = "+(status[0] instanceof Long));
            System.out.println("status[0]="+status[0]+" status[0] instanceof Integer = "+(status[0] instanceof Integer));
            System.out.println("status[1]="+status[1]);
            System.out.println("studentsMap.get(status[0])="+studentsMap.get(status[0]));
            if(status[0] instanceof Integer){
                Long id = new Long((Integer)status[0]);
                studentsMap.get(id).setLastLearningTime((Date)status[1]);
            }else if(status[0] instanceof Long){
                studentsMap.get(status[0]).setLastLearningTime((Date)status[1]);
            }
        }
        //学生们应该按照班级什么的分组，以后证明了必要性再提供这个功能（2011-10-11）
        request.setAttribute("students", students);
        forward(request, response, "/student/students.jsp");
    }

    /**
     * 学生24小时内背诵的单词。
     */
    @EJB LearningStatusEntityFacade learningStatusFacade;
    
    public void learningStatus(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String useridParam = request.getParameter("userid");
        if(useridParam == null){
            request.setAttribute("errMsg", "没有用户的id信息，无法查询其学习状况。");
        }
        Long userid = Long.valueOf(useridParam);
        List<LearningStatusEntity> learntStatus = learningStatusFacade.findLearningStatusIn24Hours(userid);
        System.out.print("StudentServlet.learningStatus() learntStatus.size() = "+learntStatus.size());
        request.setAttribute("learntStatus", learntStatus);
        request.setAttribute("student", userFacade.find(userid));
        forward(request, response, "/student/learntWords.jsp");
    }    
}
